import Vue from "vue";
import VueRouter from "vue-router";
import IndexVue from "../views/IndexVue.vue";
import "@/assets/iconfont.css";
import store from "@/store/index.js";

Vue.use(VueRouter);

const routes = [
  // 首页
  {
    path: "/",
    name: "IndexVue",
    component: IndexVue,
    meta:{
      index:0,
      meta:true
    }
    // 路由元信息：可以理解为路由描述信息，让开发者自己定义属性
  },

  // 首页 秒杀 拼团惠 普通商品 详情页
  {
    path:"/DetailsAll/:id",
    name:"DetailsAll",
    component: () => import("../views/DetailsAll.vue"),
    meta: {
      auth: false,
    },
  },
  // 首页搜索详情页
  {
    path: "/SearchInfo",
    name: "SearchInfo",
    component: () => import("../views/SearchInfo.vue"),
    meta: {
      auth: false,
    },
  },
  // 首页百货详情页
  {
    path:"/MerchInfo",
    name:"MerchInfo",
    component:()=> import("../views/MerchInfo.vue")
  },
  // 首页新闻简报详情
  {
    path:"/NewsInfo",
    name:"NewsInfo",
    component:()=> import("../views/NewsInfo.vue")
  },
  // 领取优惠卷详情页
  {
    path:"/CouponsList",
    name:"CouponsList",
    component:()=> import("../views/CouponsList.vue"),
    meta:{
      auth:true
    }
  },
  
  // 分类
  {
    path: "/ClassifyView",
    name: "ClassifyView",
    component: () => import("../views/ClassifyView.vue"),
    meta: {
      auth: false,
      index:1,
    },
  },
  // 分类详情页
  {
    path:"/ClassifyDetails",
    name:"ClassifyDetails",
    component: () => import("../views/ClassifyDetails.vue"),
    meta: {
      auth: false,
      index:1,
    },
  },

  // 购物车
  {
    path: "/ShoppingCartView",
    name: "ShoppingCartView",
    component: () => import("../views/ShoppingCartView.vue"),
    meta: {
      auth: true,
      index:2,
    },
  },
  // 购物车详情页
  {
    path: "/ProductDetails",
    name: "ProductDetails",
    component: () => import("../views/ProductDetails.vue"),
  },
  // 我的
  {
    path: "/MyView",
    name: "MyView",
    component: () => import("../views/MyView.vue"),
    meta: {
      auth: false,
      index:3,
    },
  },
  //登录
  {
    path: "/LoginPage",
    name: "LoginPage",
    component: () => import("../views/LoginPage.vue"),
    meta: {
      auth: false,
    },
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
];

const router = new VueRouter({
  routes,
});

// 导航守卫
// 所有路由跳转之前都会经过这个函数，你可以在这个函数内部判断能否访问这个路由。起到路由拦截的作用。
router.beforeEach((to, from, next) => {
  // console.log("路由导航守卫", to.meta.isLogin);
  if (to.meta.auth) {
    const token = sessionStorage.getItem("token") || undefined;
    if (token) {
      next();
    } else {
      alert("请登录");
      store.commit("setUrl", to.path);
      console.log(store.state.beforeUrl);
      next("/LoginPage");
    }
  } else {
    next();
  } 
});
export default router;
